import { _decorator, Asset, assetManager, BufferAsset, Component, loader, Node, resources } from 'cc';
import { SprotoManager } from './SprotoManager';
const { ccclass, property } = _decorator;

@ccclass('netManager')
export class netManager extends Component {
    ws:WebSocket
    async start() {
        await SprotoManager.Instance.init()
        this.ws=new WebSocket("ws://192.168.31.33:8888")
        this.ws.binaryType="arraybuffer"
        this.ws.onopen=()=>{
            console.log("建立连接")
            this.ws.send(SprotoManager.Instance.pack("set",{what:"123",value:"312"},1).buffer)
            // console.log(SprotoManager.Instance.pack("handshake",null,1).buffer)
            this.ws.send(SprotoManager.Instance.pack("handshake",null,2).buffer)
            
        }
        this.ws.onmessage=async (eva)=>{
            // console.log(eva.data)
            // console.log(await eva.data.arrayBuffer())
            const pack= SprotoManager.Instance.recv_package(eva.data)
            if(pack.type==='REQUEST'){
                console.log(pack.pname,pack.result)
            }else{
                // console.log(pack)
                console.log(pack.session,pack.result)
                if(pack.session===2){
                    this.ws.send(SprotoManager.Instance.pack("get",{what:"123"},3).buffer)
                }
            }
            
        }
        this.ws.onclose=()=>{
            console.log("连接断开")
        }
        
        // const s2csproto=assetManager.loadAny()

        // resources.load
    }

    update(deltaTime: number) {
        
    }
}

